package cn.iocoder.yudao.module.diy.controller.admin.diy;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.diy.controller.admin.diy.vo.other.*;
import cn.iocoder.yudao.module.diy.controller.admin.diy.vo.page.DiyPagePageReqVO;
import cn.iocoder.yudao.module.diy.controller.admin.page.vo.PagePageReqVO;
import cn.iocoder.yudao.module.diy.convert.DiyPageConvert;
import cn.iocoder.yudao.module.diy.dal.dataobject.addon.AddonDO;
import cn.iocoder.yudao.module.diy.dal.diy.*;
import cn.iocoder.yudao.module.diy.enums.SceneDomainVo;
import cn.iocoder.yudao.module.diy.service.addon.AddonService;
import cn.iocoder.yudao.module.diy.service.diy.NDiyPageService;
import cn.iocoder.yudao.module.diy.service.diy.ICoreDiyConfigService;
import cn.iocoder.yudao.module.diy.service.diy.IDiyService;
import cn.iocoder.yudao.module.diy.service.diyroute.DiyRouteService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.successAdmin;

/**
 * 自定义页面管理
 */
@Tag(name = "管理后台 - ns装修管理")
@RestController
@RequestMapping("/diy/set-manage")
public class NDiyController {

    @Resource
    IDiyService diyPageService;
    @Resource
    private NDiyPageService pageService;
    @Resource
    private DiyRouteService diyRouteService;
    @Resource
    ICoreDiyConfigService diyConfigService;
    @Resource
    private AddonService AddonService;

    /**
     * 自定义页面分页列表
     *
     * @param pageVO 参数
     * @return Result<Object>
     */
    //根据站点获取页面数据
    @GetMapping("/diy")
    @PermitAll
    public CommonResult<PageResult<NsDiyPageRespVo>> list(@Valid DiyPagePageReqVO pageVO) {
        if (null!=pageVO.getAddon_name()){
            pageVO.setAddonName(pageVO.getAddon_name());
        }
        PageResult<NsDiyPageRespVo> pageResult = diyPageService.nsPageList(pageVO);
        return successAdmin(DiyPageConvert.INSTANCE.convertPage2(pageResult));
    }

    /**
     * 获取自定义页面列表
     * @param pageVO
     * @return
     */
    @GetMapping("/list")
    public CommonResult<List<NsDiyPageRespVo>> allList(@Valid DiyPagePageReqVO pageVO) {
        return successAdmin(diyPageService.allList(pageVO));
    }

    /**
     * 自定义页面详情
     *
     * @param id 主键ID
     * @return Result<DiyPageInfoVo>
     */
    @GetMapping("/diy/{id}")
    public CommonResult<NsDiyPageRespVo> info(@PathVariable("id") Long id) {
        NsDiyPageRespVo info = diyPageService.info(id);
        return successAdmin(info);
    }

    /**
     * 自定义页面添加
     *
     * @param param 参数
     * @return Result<Object>
     */
    @PostMapping("/diy")
    public CommonResult<Long> add(@Valid @RequestBody DiyPageParam param) {
        return successAdmin(diyPageService.add(param));
    }

    /**
     * 自定义页面编辑
     *
     * @param param 参数
     * @return Result<Object>
     */
    @PutMapping("/diy/{id}")
    public CommonResult<Boolean> edit(@PathVariable Long id,@Valid @RequestBody DiyPageParam param) {
        diyPageService.edit(id,param);
        return successAdmin(true);
    }

    /**
     * 自定义页面删除
     *
     * @param id 参数
     * @return Result<Object>
     */
    @DeleteMapping("/diy/{id}")
    public CommonResult<Boolean> del(@PathVariable("id") Long id) {
        diyPageService.del(id);
        return successAdmin(true);
    }

    /**
     * 页面初始化数据
     * @param
     * @return
     */
    @GetMapping("/init")
    public CommonResult<NsDiyPageRespVo> getPageInit(@Valid DiyPageInitParam param) {
        return successAdmin(diyPageService.getPageInit(param));
    }
    /**
     * 存在的应用插件列表
     *
     * @return
     */
    @TenantIgnore
    @GetMapping("/apps")
    public CommonResult<Map<String, InstallAddonListVo>> info() {
        
        List<AddonDO> addonList =AddonService.getAddonList(new AddonDO());
//        AddonService.createDefaultAddon();
        Map<String, InstallAddonListVo> map = new HashMap<>();
        if (ObjectUtil.isNotEmpty(addonList)) {
            for(AddonDO item: addonList)
            {
                InstallAddonListVo installAddonListVo = new InstallAddonListVo();
                BeanUtils.copyProperties(item, installAddonListVo);
                installAddonListVo.setIcon("");
                installAddonListVo.setCover("");
                map.put(item.getAddonKey(), installAddonListVo);
            }
        }
        return successAdmin(map);
    }
    /**
     * 存在的应用插件列表
     *
     * @return
     */
    @GetMapping("/route/apps")
    public CommonResult<Map<String, InstallAddonListVo>> route() {
        List<AddonDO> addonList =AddonService.getAddonList(new AddonDO());
        Map<String, InstallAddonListVo> map = new HashMap<>();
        if (ObjectUtil.isNotEmpty(addonList)) {
            for(AddonDO item: addonList)
            {
                InstallAddonListVo installAddonListVo = new InstallAddonListVo();
                BeanUtils.copyProperties(item, installAddonListVo);
                installAddonListVo.setIcon("");
                installAddonListVo.setCover("");
                map.put(item.getAddonKey(), installAddonListVo);
            }
        }
        return successAdmin(map);
    }

    /**
     * 获取自定义链接列表
     * @return
     */
    @GetMapping("/link")
    public CommonResult<JSONObject> getLink() {
        return successAdmin(diyPageService.getLink());
    }

    @Data
    public static class Id {
        private Integer id;
    }

    /**
     * 设为使用
     *
     * @return Result<Object>
     */
    @PutMapping("/use")
    public CommonResult<Object> setUse(@PathVariable("id") Long id) {
        diyPageService.setUse(id);
        return successAdmin(true);
    }

    /**
     * 获取页面模板
     * @return
     */
    @GetMapping("/template")
    public CommonResult<JSONObject> getTemplate(TemplateParam param) {
        return successAdmin(diyPageService.getTemplate(param));
    }

    /**
     * 获取模板页面
     * @param type
     * @param mode
     * @return
     */
    @GetMapping("/template/pages")
    public CommonResult<JSONObject> getTemplatePages(@RequestParam(name = "type", defaultValue = "") String type, @RequestParam(name = "mode", defaultValue = "") String mode) {
        return successAdmin(PagesEnum.getPages(type, mode));
    }
    /**
     * 切换模板
     * @param param
     * @return
     */
    @PutMapping("/change")
    public CommonResult<Object> changeTemplate(@Validated @RequestBody StartUpPageConfigParam param) {
        if(null==param.getSiteId()){
            param.setSiteId(TenantContextHolder.getTenantId());
        }
        diyPageService.changeTemplate(param);
        return successAdmin(true);
    }

    /**
     * 获取页面装修列表
     * @param searchParam
     * @return
     */
    @GetMapping("/decorate")
    public CommonResult<JSONObject> getDecoratePage(DiyPageSearchParam searchParam) {
        return successAdmin(diyPageService.getDecoratePage(searchParam));
    }

    /**
     * 自定义页面分页列表，轮播搜索组件用
     * @return
     */
    @GetMapping("/carousel_search")
    public CommonResult<PageResult<NsDiyPageRespVo>> getPageByCarouselSearch(@Validated PagePageReqVO pageReqVO) {
        return successAdmin(diyPageService.getPageByCarouselSearch(pageReqVO));
    }

    /**
     * 复制
     * @param id
     * @return
     */
    @PostMapping("/copy")
    public CommonResult<Object> copy(@RequestBody Id id) {
        diyPageService.copy(id.getId());
        return successAdmin(true);
    }

    /**
     * 自定义路由列表
     *
     * @param searchParam 搜索参数
     * @return Result<Object>
     */
    @GetMapping("/route")
    public CommonResult<List<DiyRouteListVo>> list(@Validated DiyRouteSearchParam searchParam) {
        return successAdmin(diyRouteService.list(searchParam));
    }

    /**
     * 获取url
     */
    @GetMapping("/url")
    public CommonResult<SceneDomainVo> getSceneDomainVo() {
        return successAdmin(diyPageService.getSceneDomain());
    }

    /**
     * 底部导航配置列表
     *
     * @return
     */
    @GetMapping("/bottom")
    public CommonResult<List<BottomConfigVo>> getBottomList() {
        return successAdmin(diyConfigService.getBottomList());
    }

    /**
     * 底部导航配置
     *
     * @return
     */
    @GetMapping("/bottom/config")
    public CommonResult<BottomConfigVo> getBottomConfig(@RequestParam("key") String key) {
        if(key == null|| key.equals("")) key = "app";
        return successAdmin(diyConfigService.getBottomConfig(TenantContextHolder.getTenantId(),key));
    }

    /**
     * 设置底部导航
     *
     * @return
     */
    @PostMapping("/bottom")
    public CommonResult setBottomConfig(@RequestBody SetBottomConfigParam param){
        if(null==param.getSiteId()){
            param.setSiteId(TenantContextHolder.getTenantId());
        }
        diyConfigService.setBottomConfig(TenantContextHolder.getTenantId(),param.getSiteId(),param.getValue(),param.getKey());
        return successAdmin(true);
    }


}
